// JavaScript Document
//查询系统接口参数
$(function(){
    getDicParamApi();
    baiduDomainDNS_Hide();
});

function getDicParamApi(){
    var service = {};
    var fn="getDicParamApi";
    service = Commonjs.jsonToString(service);
    var params = Commonjs.getParams(fn,service);//获取参数
    Commonjs.ajaxTrue(sysurl,params,getApiSuccess);
}

var notIn = ['winicpurl_query', 'winicpurl','mailssl','authParamUrl','openQQLogin'];

function getApiSuccess(data){
    if(data.data == null){
        return false;
    }
    if (data.data.length>0){
        $.each(data.data, function (index, item) {
            if (""!=item.paramValue && $.inArray(item.paramName, notIn) < 0 && item.paramName != "qqlogo" && item.paramName != "toolServerId" && item.paramName != "openQQLogin") {
                data.data[index].paramValue = cloudEncrypt.decodeSession(item.paramValue);
            }
            if ("smsServer" == item.paramName) {
                vmInterface.smsServer = item.paramValue;
            }
        })
    }
    $("#userlist:text").empty();
    var userlist="";
    if (data.data.length>0){
        //获取所有input
        $("input:text").each(function(){
            //将input中获取的id赋值
            var paramName = $(this).attr("id");
            //循环json
            BaseForeach(data.data,function(i,item){
                if(item.paramName == "alipayac_way"){
                    if(item.paramValue == 2){
                        $("#alipayac_way").val(2);
                        $("#alipayachide").hide();
                    }
                }
                if(item.paramName == "ifOpenSms"){
                    if(item.paramValue == 2){
                        $("#ifOpenSms").val(2);
                    }
                }
                if(item.paramName == "IRCSIsStart"){
                    if(item.paramValue == 2){
                        $("#IRCSIsStart").val(2);
                    }
                }

                if(item.paramName == "ifOpenWinMyDns"){
                    if(item.paramValue == "2"){
                        $("#ifOpenWinMyDns").val(2);
                        $("#WinMyDnsUrlID").hide();
                        $('#WinMyDnsCertSet').hide();
                    }
                }

                if(item.paramName == "ifOpenBaiduDomainMgr"){
                    $("#ifOpenBaiduDomainMgr").val(item.paramValue);
                    if(item.paramValue == "1"){
                        baiduDomainDNS_Show();
                    }
                }
                //如果input的id跟json中的name活相等

                if(paramName == item.paramName){
                    //赋值
                    $("#"+paramName).val(item.paramValue);
                }
                if(item.paramName == "qqlogo"){
                $("#SmallImglogo").attr('src',item.paramValue);
            }
            if(item.paramName == "toolServerId"){
                vmInterface.toolServerId = parseInt(item.paramValue) ;
            }
            });
        });

        //获取所有select
        $("select").each(function(){
            //将input中获取的id赋值
            var paramName = $(this).attr("id");
            //循环json
            BaseForeach(data.data,function(i,item){
                //如果input的id跟json中的name活相等
                if(paramName == item.paramName){
                    //赋值
                    $("#"+paramName).val(item.paramValue);
                }

                if (item.paramName == "openQQLogin") {
                    if (item.paramValue == 'Y') {
                        $('#openQQLogin').val("Y");
                    }
                }
            });
        });

        $("input:password").each(function(){
            //将input中获取的id赋值
            var paramName = $(this).attr("id");
            //循环json
            BaseForeach(data.data,function(i,item){
                if(item.paramName == "alipayac_way"){
                    if(item.paramValue == 2){
                        $("#alipayac_way").val(2);
                        $("#alipayachide").hide();
                    }
                }
                if(item.paramName == "ifOpenSms"){
                    if(item.paramValue == 2){
                        $("#ifOpenSms").val(2);
                    }
                }
                if(item.paramName == "IRCSIsStart"){
                    if(item.paramValue == 2){
                        $("#IRCSIsStart").val(2);
                    }
                }

                //如果input的id跟json中的name活相等
                if(paramName == item.paramName){
                    //赋值
                    $("#"+paramName).val(item.paramValue);
                }
            });
        });
    }else{
        userlist+=' <tr><td colspan="10" style="height:50px;text-align:center;line-height:50px;">找不到相关信息</td></tr>';
    }
    $('#userlist').append(userlist);
}

//保存参数参数
function uptDicParamApi(){
    var str1="";
    var request = [];
    $("input:text").each(function(index, element) {
        var val = $(this).val();
        if (val && ''!=val && $.inArray($(this).attr("id"), notIn) < 0) {
            val = cloudEncrypt.encodeSession(val);
        }
        str1+='{"paramName":"'+$(this).attr("id")+'","paramValue":"'+val+'"},';
        var item = {
            paramName: $(this).attr("id"),
            paramValue: val
        };
        request.push(item);
    });
    $("select").each(function(index, element) {
        var val = $(this).val();
        if (val && ''!=val && $.inArray($(this).attr("id"), notIn) < 0 && val != "openQQLogin" ) {
            val = cloudEncrypt.encodeSession(val);
        }
        str1+='{"paramName":"'+$(this).attr("id")+'","paramValue":"'+val+'"},';
        var item = {
            paramName: $(this).attr("id"),
            paramValue: val
        };
        request.push(item);
    });
    if($("#SmallImglogo").attr("src") != "../img/show.jpg"){
        var item = {
            paramName: "qqlogo",
            paramValue: $("#SmallImglogo").attr("src")
        };
        request.push(item);
    }
    if(vmInterface.toolServerId != ''){
        var item = {
            paramName: "toolServerId",
            paramValue: vmInterface.toolServerId
        };
        request.push(item);
    }
    $("input:password").each(function(index, element) {
        var val = $(this).val();
        if (val && ''!=val && $.inArray($(this).attr("id"), notIn) < 0) {
            val = cloudEncrypt.encodeSession(val);
        }
        str1 +='{"paramName":"'+$(this).attr("id")+'","paramValue":"'+val+'"},';
        var item = {
            paramName: $(this).attr("id"),
            paramValue: val
        };
        request.push(item);
    });
    var fn="uptDicParamApi";
    str1 = str1.substr(0,str1.length-1);//除去最后一个“，”
    var service = Commonjs.jsonToString(request);
    //var params = Commonjs.getParams(fn,"["+str1+"]");//获取参数
    var params = Commonjs.getParams(fn, service);
    Commonjs.ajaxTrue(sysurl,params,uptApiSuccess);
}

function uptApiSuccess(data){
    topSuccess(window, data.msg);
    getDicParamApi();
}

function onclickselect(){
    if($("#alipayac_way").val() == 2){
        $("#alipayachide").hide();
    }else{
        $("#alipayachide").show();
    }
}

function WinMyDnsShow(){
    if($("#ifOpenWinMyDns").val() == 1){
        $("#WinMyDnsUrlID").show();
        $('#WinMyDnsCertSet').show();
    }else{
        $("#WinMyDnsUrlID").hide();
        $('#WinMyDnsCertSet').hide();
    }
}

function baiduDomainShow(){
    console.log($("#ifOpenBaiduDomainMgr").val());
    if($("#ifOpenbaiduDomainMgr").val() == 1){
        baiduDomainDNS_Show();
    }else{
        baiduDomainDNS_Hide();
    }
}

function baiduDomainDNS_Show(){
    $("#baiduDomainDNS").show();
}

function baiduDomainDNS_Hide(){
    $("#baiduDomainDNS").hide();
}

function upload(id,image) {
    var filename = $("#"+id).val();
    var index = filename.lastIndexOf('.');
    var type = filename.substring(index+1,filename.length);
    if(type.toLowerCase() != 'jpg' && type.toLowerCase() != 'gif'
        && type.toLowerCase() != 'png'&&type.toLowerCase() != 'jpeg'){
        topError(window, '注意喔：图片格式必须为.jpeg|.gif|.jpg|.png');
        return ;
    }
    var arrID = [ id ];
    $.yihuUpload.ajaxFileUpload( {
        url : realPath+'/upload.do', // 用于文件上传的服务器端请求地址
        secureuri : false,           // 一般设置为false
        type:"POST",
        fileElementId : arrID,       // 文件上传空间的id属性 <input type="file" id="file" name="file" />
        dataType : 'json',           // 返回值类型 一般设置为json
        upname:'config',
        success : function(data, status) {
            var uri = data.url;
            uri=uri.replace('fullsize','small');
            var name = data.NewFileName;
            var fname = data.FileName;
            var size = data.Size;
            var old = $("#" + id + "_f");
            $("#SmallImglogo").attr("src", uri);
            $("#qqlogo").val(uri);

            // if (image=='SmallImgV') {
            //     $("#SmallImglogo").attr("src", uri);
            //     $("#qqlogo").val(uri);
            // }else if(image=='SmallImglogo'){
            //     $("#SmallImglogo").attr("src", uri);
            //     $("#wechatlogo").val(uri);
            // }
        },
        error : function(data, status, e) {
            topError(window, '图片上传失败：建议您选择不超过1M的图片且在良好的网络环境下继续上传');
        }
    });
}

var vmInterface = new Vue({
    el: '#main-block',
    data: function () {
        return {
            smsServer: 1,
            toolServerList:[],
            toolServerId:''
        }
    },
    methods: {
        getToolServerList:function(){
            var self = this;
            var service = {
                serviceType: 2
            };
            var fn="mimiServiceSelect";
            service = Commonjs.jsonToString(service)
            var params = Commonjs.getParams(fn,service);
            Commonjs.ajaxSilence(weburl,params,true,function (data) {
                self.toolServerList = data.data;
            }, function (data) {
                self.$message.error(data.msg);
            });
        }
    },
    mounted: function () {
        this.getToolServerList();
    }
});
